package com.xuef.service.impl;

import com.xuef.entity.*;
import com.xuef.repository.BlogRepository;
import com.xuef.repository.CommentRepository;
import com.xuef.repository.UserRepository;
import com.xuef.service.BlogService;
import com.xuef.service.EsBlogService;
import com.xuef.util.JedisAdapter;
import com.xuef.util.RedisKeyGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by moveb on 2018/5/7.
 */
@Service("blogService")
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private EsBlogService esBlogService;
    @Autowired
    JedisAdapter jedisAdapter;

    @Override
    public Blog saveBlog(Blog blog) {
        // 新增博文
        boolean isNew = (blog.getId() == null);
        EsBlog esBlog = null;

        Blog returnBlog = blogRepository.save(blog);

        // 同步更新 elasticsearch 文库
        if (isNew) {
            esBlog = new EsBlog(returnBlog);
            // 设置elasticsearch中 esblog id 为 blog的id
            esBlog.setId(returnBlog.getId()+"");
        } else {
            esBlog = esBlogService.getEsBlogByBlogId(blog.getId());
            esBlog.update(returnBlog);
        }
        esBlogService.updateEsBlog(esBlog);
        return returnBlog;
    }

    @Override
    public void removeBlog(int id) {
        // delete mysql中的blog
        blogRepository.delete(id);
        // 删除es中对应的blog
        esBlogService.removeEsBlog(String.valueOf(id));
    }

    @Override
    public Blog getBlogById(int id) {
        return blogRepository.findOne(id);
    }

    @Override
    public Page<Blog> listBlogsByUserAndKeywordLike(User user, String title, Pageable pageable) {
        // 模糊查询
        title = "%" + title + "%";
        //Page<Blog> blogs = blogRepository.findByUserAndTitleLikeOrderByCreateTimeDesc(user, title, pageable);
        String tags = title;
        Page<Blog> blogs = blogRepository.findByTitleLikeAndUserOrTagsLikeAndUserOrderByCreateTimeDesc(title,user, tags,user, pageable);
        return blogs;
    }

    @Override
    public Page<Blog> listBlogsByTitleVoteAndSort(User user, String title, Pageable pageable) {
        // 模糊查询
        title = "%" + title + "%";
        Page<Blog> blogs = blogRepository.findByUserAndTitleLike(user, title, pageable);
        return blogs;
    }

    @Override
    public Page<Blog> listBlogsByCatalog(Catalog catalog, Pageable pageable) {
        Page<Blog> blogs = blogRepository.findByCatalog(catalog, pageable);
        return blogs;
    }

    @Override
    public List<Object[]> getBlogCount(int userId) {
        return blogRepository.getBlogCount(userId);
    }

    @Override
    public Page<Blog> listBlogsByYearMonth(User user, Date start, Date end, Pageable pageable) {
        return blogRepository.findByUserAndCreateTimeGreaterThanEqualAndCreateTimeLessThanEqual(user, start, end, pageable);
    }

    @Override
    public Page<Blog> findByIdIn(List<Integer> blogs, Pageable pageable){
        return blogRepository.findByIdIn(blogs, pageable);
    }
    /**
     *
     * @param id 博客文章id
     */
    @Override
    public void readingIncrease(int id) {
        String pvKey = RedisKeyGenerateUtil.getPvKey(id);
        String pv = jedisAdapter.get(pvKey);
        int pvVal = 0;
        if(pv == null){
            jedisAdapter.set(pvKey, String.valueOf(1));
        }else{
            pvVal = Integer.parseInt(pv);
            jedisAdapter.set(pvKey, String.valueOf(++pvVal));
        }
    }

    @Override
    public int getReadCount(int blogId) {
        String pvKey = RedisKeyGenerateUtil.getPvKey(blogId);
        String pv = jedisAdapter.get(pvKey);
        if(pv == null){
            return 0;
        }else{
            return Integer.valueOf(pv);
        }
    }

    @Override
    public Comment saveComment(Comment comment) {
        Comment savedComment = commentRepository.save(comment);
        updateCommentCount(comment.getEntityId());// blogId
        return savedComment;
    }

    @Override
    public List<Comment> listComments(int entityType, int entityId) {
        List<Comment> comments = commentRepository.findByEntityTypeAndEntityId(entityType, entityId);

        return comments;
    }

    @Override
    public void removeComment(int blogId, int commentId) {

    }

    @Override
    public void updateCommentCount(int blogId) {
        blogRepository.updateCommentCount(blogId);
    }

    @Override
    public Blog createVote(int blogId) {
        return null;
    }

    @Override
    public void removeVote(int blogId, int voteId) {

    }


}
